﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jalal.Library.Data;
using System.Data.Common;

namespace BCGC.DAL
{
    public class MarketPlaceData
    {
        public static DataTable GetAllMarketListing()
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetAllMarketPlaceData";
            return dataMiner.GetListData(command);
        }

        public static DataTable GetMarketByID(int id)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetMarketPlaceByID";
            dataMiner.AddParameter(command, "@ItemID", DbType.Int32, 0, ParameterDirection.Input, id);
            return dataMiner.GetListData(command);
        }

        public static bool UpdateMarket(int itemID, bool isActive, string adminComment, string marketContent, bool showContactInfo, string contactInformation)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateMarketPlace";
            dataMiner.AddParameter(command, "@ItemID", DbType.Int32, 0, ParameterDirection.Input, itemID);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@AdminComment", DbType.String, 0, ParameterDirection.Input, adminComment);
            dataMiner.AddParameter(command, "@MarketContent", DbType.String, 400, ParameterDirection.Input, marketContent);
            dataMiner.AddParameter(command, "@ShowContactInfo", DbType.Boolean, 0, ParameterDirection.Input, showContactInfo);
            dataMiner.AddParameter(command, "@ContactInfo", DbType.String, 0, ParameterDirection.Input, contactInformation);            
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return false;
            else
                return (((int)count==0)?false:true);
            
        }

        public static int CreateMarketPlace(int userID, string title, string marketContent, DateTime date, bool showContactInfo, bool isActive, string adminComment, string contactInformation)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreateMarketPlace";
            dataMiner.AddParameter(command, "@UserID", DbType.Int32, 0, ParameterDirection.Input, userID);
            dataMiner.AddParameter(command, "@Title", DbType.String, 100, ParameterDirection.Input, title);
            dataMiner.AddParameter(command, "@MarketContent", DbType.String, 400, ParameterDirection.Input, marketContent);
            dataMiner.AddParameter(command, "@Date", DbType.DateTime, 0, ParameterDirection.Input, date);
            dataMiner.AddParameter(command, "@ShowContactInfo", DbType.Boolean, 0, ParameterDirection.Input, showContactInfo);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@AdminComment", DbType.String, 100, ParameterDirection.Input, adminComment);
            dataMiner.AddParameter(command, "@ContactInfo", DbType.String, 0, ParameterDirection.Input, contactInformation);            
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return 0;
            else
                return Convert.ToInt32(count);
        }

        public static bool DeleteMarketPlace(int itemID)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeleteMarketPlace";
            dataMiner.AddParameter(command, "@ItemID", DbType.Int32, 0, ParameterDirection.Input, itemID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return false;
            else
                return (((int)count == 0) ? false : true);
        }

        public static DataTable GetAllMarketListing(string userName, DateTime? dtFrom, DateTime? dtTo, bool? isActive, int currentPageIndex, int pageSize)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_SearchAllMarketPlaceData";
            dataMiner.AddParameter(command, "@UserName", DbType.String, 0, ParameterDirection.Input, "%"+userName+"%");
            dataMiner.AddParameter(command, "@FromDate", DbType.DateTime, 0, ParameterDirection.Input, dtFrom);
            dataMiner.AddParameter(command, "@ToDate", DbType.DateTime, 0, ParameterDirection.Input, dtTo);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPageIndex);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
            return dataMiner.GetListData(command);
        }
    }
}
